package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.Explain;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.ExplainService;
import com.example.toolproject.demo.util.FileUploadAndDownload;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ExplainController
 * @Description TODO   该请求是对于登录后，实验说明的配置，包括修改和查看
 * @Author lvyongwei
 * @Date 2020-08-26 17:25
 **/
@Controller
@CrossOrigin
@RequestMapping("/explain")
public class ExplainController {

    @Autowired
    private ExplainService explainService;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    @GetMapping("/getExplainById")
    @ResponseBody
    public ResultBean getExplainById(Integer id, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //System.out.println(id);
        try{
            Explain explain=explainService.getExplainById(id);
            return ResultBean.success("获取数据成功",explain);
        }catch (Exception e){
            return ResultBean.error("获取数据失败");
        }
    }


    /**
     * 修改内容
     * @param file
     * @param explain
     * @return
     */
    @PostMapping("/updateExplain")
    @ResponseBody
    public ResultBean insertScenicArea(@RequestParam(value = "file" , required = false) MultipartFile file, Explain explain, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        if(StringUtils.isEmpty(file) || file==null){
            try{
                explainService.updateExplain(explain);
                return ResultBean.success("修改内容成功");
            }catch (Exception e){
                return ResultBean.error("修改内容失败");
            }
        }
        String fileName="";
        if(file.getSize()==0){
            return ResultBean.error("请上传文件");
        }else{
            /**
             * 文件上传
             */
            fileName=fileUploadAndDownload.upload(file,"\\explain");
        }
        explain.setPath("\\explain\\"+fileName);
        try{
            explainService.updateExplain(explain);
            return ResultBean.success("修改内容成功");
        }catch (Exception e){
            return ResultBean.error("修改内容失败");
        }
    }
}
